(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initSubject(currentPage,pageSize);
		});
	});
	function initSubject(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var type = $("#subject-type option:selected").val();
		var data = {
				cmd:"getTeahcerForCourse",
				model:"course",
				userKey:userKey,
				data:{
					currentpage:currentpage,
					pagesize:pagesize,
					type:type,
					schoolcode:schoolcode,
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.data;
				var total = res.data.total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#student-page").empty();
				$("#student-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initSubject(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initSubject((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize%5 == 0){
							if((now+1) < totalSize/5){
								now ++;
								initSubject((now*5+1),pageSize);
							}else{
								return false;
							}
						}else{
							if((now+1) <= (totalSize-totalSize%5)/5){
								now ++;
								initSubject((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					}
				});
				if(data.length > 0){
					dataCache = [];
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						var subjectType = "";
						if(data[i].type == 0){
							subjectType = "小学科目";
						}else if(data[i].type == 1){
							subjectType = "中学科目";
						}else if(data[i].type == 2){
							subjectType = "中学科目";
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:120px;'>"+data[i].coursecontent + "</div>" +
								"<div class=\"table-code\" style='width:120px;'>"+data[i].subjectcode+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
								"<div class=\"table-class\" style='width:150px;'>"+subjectType+"</div>" +
								"<div class=\"table-code\" style='width:150px;'>类型</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">课程科目</div>" +
								"<div class=\"table-oprate\">" +
									"<div class=\"table-detail\" dataindex = \""+i+"\" subjectcode=\""+data[i].subjectcode+"\" title=\"详情\"></div>" +
									"<div class=\"table-edit\" dataindex = \""+i+"\" subjectcode=\""+data[i].subjectcode+"\" title=\"编辑\"></div>" +
									"<div class=\"table-delete\" subjectcode=\""+data[i].subjectcode+"\" title=\"删除\"></div>" +
								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#subject-data-show").empty();
					$("#subject-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					
					$(".table-detail").unbind("click");
					$(".table-detail").click(function(){
						var index = parseInt($(this).attr("dataindex"));
						$("#subject-form-show").show();
						$("#right-tables").hide();
						$("#subject-form-show").contents().find("input").attr("disabled","disabled");
						$("#subject-form-show").contents().find("input").css("background-color","#E5E5E5");
						$("#subject-form-show").contents().find("select").attr("disabled","disabled");
						$("#subject-form-show").contents().find("select").css("background-color","#E5E5E5");
						$("#subject-form-show").contents().find("#back-to-table").unbind("click");
						$("#subject-form-show").contents().find("#back-to-table").click(function(){
							$("#subject-form-show").hide();
							$("#right-tables").show();
						});
						showData(dataCache[index],"detail");
					});
					
					$(".table-edit").unbind("click");
					$(".table-edit").click(function(){
						var index = parseInt($(this).attr("dataindex"));
						$("#subject-form-show").show();
						$("#right-tables").hide();
						$("#subject-form-show").contents().find("input").removeAttr("disabled");
						$("#subject-form-show").contents().find("input").css("background-color","#fff");
						$("#subject-form-show").contents().find("select").removeAttr("disabled");
						$("#subject-form-show").contents().find("select").css("background-color","#fff");
						$("#subject-form-show").contents().find("#subject-subjectcode").attr("disabled","disabled");
						$("#subject-form-show").contents().find("#subject-subjectcode").css("background-color","#E5E5E5");
						$("#subject-form-show").contents().find("#back-to-table").unbind("click");
						$("#subject-form-show").contents().find("#back-to-table").click(function(){
							$("#subject-form-show").hide();
							$("#right-tables").show();
						});
						showData(dataCache[index],"edit");
						$("#subject-form-show").contents().find("#save-btns").unbind("click");
						$("#subject-form-show").contents().find("#save-btns").click(function(){
							if(checkSubject() && check2("check-fail","subject-form-show")){
								updateSubject();
							}
						});
						
						check1("subject-coursecontent",isNull,"null","subject-form-show");
					});
					
					$(".table-delete").unbind("click");
					$(".table-delete").click(function(){
						var subjectcode = $(this).attr("subjectcode");
						deleteBox("确定要删除吗？",function(){
							deleteSubject(subjectcode);
						});
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#subject-data-show").empty();
					$("#subject-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#subject-data-show").empty();
				$("#subject-data-show").append(content);
			}
		},null,null);	
	}
	function updateSubject(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var params = {};
		var param = {};
		params["subjectcode"] = encodeURI($("#subject-form-show").contents().find("#subject-subjectcode").val());			
		params["coursecontent"] = encodeURI($("#subject-form-show").contents().find("#subject-coursecontent").val());
		params["type"] = $("#subject-form-show").contents().find("#subject-type option:selected").val();
		for(var key in params){
			if(params[key] != ""){
				param[key] = params[key];
			}
		}
		var data = {
				cmd:"updateTeahcerForCourse",
				model:"course",
				userKey:userKey,
				data:param,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("编辑成功");
				initSubject(currentPage,pageSize);
			}else{
				processError("编辑失败");
			}
		},null,null);
	}
	function deleteSubject(subjectcode){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"delCourseSubjectCode",
				model:"course",
				userKey:userKey,
				data:{
					subjectcode:subjectcode,
				},
				remark:"",						
		};
		processBegin("正在删除中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("删除科目成功");
				initSubject(currentPage,pageSize);
			}else{
				processError("删除科目失败");
			}
		},null,null);
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function showData(data,type){
		if(type == "detail"){
			$("#subject-form-show").contents().find("#save-btns-content").hide();
		}else if(type == "edit"){
			$("#subject-form-show").contents().find("#save-btns-content").show();
		}
		for(var key in data){
			if($("#subject-form-show").contents().find("#subject-" + key)[0]){
				if($("#subject-form-show").contents().find("#subject-" + key)[0].tagName.toLocaleLowerCase() == "input"){
					$("#subject-form-show").contents().find("#subject-" + key).val(data[key]);
				}else if($("#subject-form-show").contents().find("#subject-" + key)[0].tagName.toLocaleLowerCase() == "select"){
					var len = $("#subject-form-show").contents().find("#subject-" + key+" option");
					for(var i = 0; i < len.length ; i++){
						if(len[i].value == data[key]){
							len[i].selected = true;
						}
					}
				}
			}
		}
	}
	
	function checkSubject(){
		if($("#subject-form-show").contents().find("#subject-coursecontent").val() == ""){
			$("#subject-form-show").contents().find("#subject-coursecontent-ex").html("不能为空");
			return false; 
		}else{
			return true;
		}
	}
	function pagebtns(){
		document.onkeydown = function(e){ 
			$("select").blur();
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	now = 0;
		    	initSubject(1,pageSize);
		    }
		};
		$("#subject-search-btn").click(function(){
			now = 0;
			initSubject(1,pageSize);
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();